#ifndef __market_clock
#define __market_clock

#include "chrono.h"
#include <iostream>

class market_clock { // for display as HH:MM:SS.mmm
  
  int hours; 
  int minutes;
  int seconds;
  double milliseconds;
  
  double initial; // from a chrono
  
  public:
  
    market_clock (int _h, int _m, int _s, int _ms = 0) {
      hours = _h;
      minutes = _m;
      seconds = _s;
      milliseconds = _ms;
    }
    
    inline void reset (const chrono& c) {
      initial = c.secs;
    }
    
    void tick (const chrono& c) { 
      double elapsed = c.secs - initial;
      if (elapsed >= 0.001) {
        initial = c.secs;
        milliseconds += (elapsed / 0.001);
        if (milliseconds >= 1000) {
          milliseconds -= 1000;
          ++seconds;
          if (seconds >= 60) {
            seconds = 0;
            ++minutes;
            if (minutes >= 60) {
              minutes = 0;
              ++hours;
              if (hours >= 24) {
                hours = 0;
              }
            }
          }
        }
      }
    }
    
    friend std::ostream& operator<< (std::ostream& os, const market_clock& mc);
  
};

#endif
